<?php

namespace App\Models;

use App\Exceptions\InternalException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid;

class Order extends Model
{
    const REFUND_STATUS_PENDING='pending';
    const REFUND_STATUS_APPLIED='applied';
    const REFUND_STATUS_PROCESSING='processing';
    const REFUND_STATUS_SUCCESS='success';
    const REFUND_STATUS_FAILED='failed';
    
    const SHIP_STATUS_PENDING='pending';
    const SHIP_STATUS_DELIVERED='delivered';
    const  SHIP_STATUS_RECEIVED='received';
    
    public static $refundStatusMap=[
        self::REFUND_STATUS_PENDING=>'未退款',
        self::REFUND_STATUS_APPLIED=>'已申请退款',
        self::REFUND_STATUS_PROCESSING=>'退款中',
        self::REFUND_STATUS_SUCCESS=>'退款成功',
        self::REFUND_STATUS_FAILED=>'退款失败',
    ];
    public static $shipStatusMap=[
        self::SHIP_STATUS_PENDING=>'未发货',
        self::SHIP_STATUS_DELIVERED=>'已发货',
        self::SHIP_STATUS_RECEIVED=>'已收货',
    ];
    protected $fillable=[
        'no','address','total_amount','remark','paid_at',
        'payment_method','payment_no','refund_status',
        'refund_no','closed','reviewed','ship_status','ship_data',
        'extra'
    ];
    protected $casts=[
        'closed'=>'boolean',
        'reviewed'=>'boolean',
        'address'=>'json',
        'ship_data'=>'json',
        'extra'=>'json',
    ];
    protected $dates=[
        'paid_at'
    ];
    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        //监听模型创建事件,在写入数据库之前触发
        static::creating(function ($model){
            //如果模型的no 字段为空
            if(!$model->no){
                $model->no=static::findAvailableNo();
                if(!$model->no){
                    return false;
                }
            }
        });
    }
    //与用户关联
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    //与订单详情关联
    public function items(){
        
        return $this->hasMany(OrderItem::class);
    }
    public static function findAvailableNo(){
        $prefix=date('YmdHis');
        for ($i=0;$i<10;$i++){
          $no=$prefix.str_pad(random_int(0, 999999),6,'0',STR_PAD_LEFT);
          if(!static::query()->where('no',$no)->exists()){
              return $no;
          }
        }
        Log::warning('find order no failed');
        return false;
    }
    
    /**
     * 创建生成唯一的退款订单号
     */
    public  static  function getAvailableRefundNo()
    {
        do{
            $no=Uuid::uuid4()->getHex();
        }while(self::query()->where('refund_no',$no)->exists());
        return $no;
   }
    
    /**定义订单与优惠券的关联关系
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function couponCode()
    {
        return $this->belongsTo(CouponCode::class);
   }

}
